package com.dong.web.clazz.bean;

import java.lang.reflect.Field;
import java.util.List;

import com.dong.web.annotation.Auto;
import com.dong.web.clazz.ClazzHandler;

public class SBean {

	private String name;

	private boolean singleton = true;

	private Class<?> clazz = null;

	private Object object = null;

	private List<Field> fields = null;

	private List<String> interfaces = null;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isSingleton() {
		return singleton;
	}

	public void setSingleton(boolean singleton) {
		this.singleton = singleton;
	}

	public Class<?> getClazz() {
		return clazz;
	}

	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}

	public Object getObject() {
		if ((object == null || !singleton) && clazz != null) {
			try {
				object = clazz.newInstance();
				if (fields != null) {
					for (Field field : fields) {
						Auto auto = field.getAnnotation(Auto.class);
						String key = null;
						if (auto.value().length() > 0) {
							key = auto.value();
						} else {
							key = field.getType().getName();
						}
						field.setAccessible(true);
						field.set(object, ClazzHandler.DONG_BEAN.get(key)
								.getObject());
					}
				}
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return object;
	}

	public List<Field> getFields() {
		return fields;
	}

	public void setFields(List<Field> fields) {
		this.fields = fields;
	}

	public List<String> getInterfaces() {
		return interfaces;
	}

	public void setInterfaces(List<String> interfaces) {
		this.interfaces = interfaces;
	}

}
